package com.hyacinth.repository.jpa;

import com.hyacinth.entity.Resource;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Class Description: 资源数据持久层
 *
 * @Version v1.0 2017/4/22 13:42
 * @JDK version 1.8.0_51
 * @Author zfb
 */
@Repository
public interface ResourceRepository extends BaseRepository<Resource, String>,
        PagingAndSortingRepository<Resource, String>, JpaSpecificationExecutor<Resource> {

    /**
     * 根据角色编码查询资源集合
     *
     * @param roleCode 角色编码
     * @param dataFlag 删除状态
     * @return List<Resource> 资源集合
     */
    @Query(value = "FROM Resource r WHERE r.dataFlag=?2 and r.code IN" +
            " (SELECT rr.resCode FROM ResourceRole rr WHERE rr.roleCode=?1)")
    List<Resource> findResByRoleCode(String roleCode, String dataFlag);

    /**
     * 查询已删除或异常、资源级别是btn或是func的资源、且是禁用的
     */
    @Query("from Resource r WHERE r.level = ?1 and (r.resFlag in (?2) or r.dataFlag=?3)")
    List<Resource> findAllBanRes(int level, List<String> resFlag, String dataFlag);

    List<Resource> findByResTypeAndParentCode(String resType, String parentCode);

}
